#include "initio.hh"

int xmlSaveTIOSignals(int tioSignalNum, const GripperStateUnit* gripperSignals)
{
#if 0
    if (tioSignalNum <= 0 || gripperSignals == NULL)
    {
        return -1;
    }

    try
    {
        char sz_temp[32];
        int actualSigNum = 0;
        const char* filePath = ZUC_USER_CONFIG_DIR "/tiosignals.xml";
        /* create the document and root node*/
        xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
        doc->encoding = BAD_CAST(strdup("UTF-8"));
        xmlNodePtr root = xmlNewNode(NULL, BAD_CAST("Signals"));

        /* add the root to the document*/
        xmlDocSetRootElement(doc, root);
        for (int i = 0; i < tioSignalNum; ++i)
        {
            if (strcmp(gripperSignals[i].sigName, "") != 0)
            {
                xmlNodePtr nodeSig = xmlNewNode(NULL, BAD_CAST("Signal"));
                xmlNewProp(nodeSig, BAD_CAST("Name"), BAD_CAST(gripperSignals[i].sigName));
                bzero(sz_temp, sizeof(sz_temp));
                sprintf(sz_temp, "%d", gripperSignals[i].chnId);
                xmlNewProp(nodeSig, BAD_CAST("ChnId"), BAD_CAST(sz_temp));
                bzero(sz_temp, sizeof(sz_temp));
                sprintf(sz_temp, "%d", gripperSignals[i].type);
                xmlNewProp(nodeSig, BAD_CAST("Type"), BAD_CAST(sz_temp));
                bzero(sz_temp, sizeof(sz_temp));
                sprintf(sz_temp, "%d", gripperSignals[i].addr);
                xmlNewProp(nodeSig, BAD_CAST("Addr"), BAD_CAST(sz_temp));
                bzero(sz_temp, sizeof(sz_temp));
                sprintf(sz_temp, "%d", (int)gripperSignals[i].freq);
                xmlNewProp(nodeSig, BAD_CAST("Frequency"), BAD_CAST(sz_temp));
                xmlAddChild(root, nodeSig);
                ++actualSigNum;
            }
        }

        /* set the gripper signal number*/
        bzero(sz_temp, sizeof(sz_temp));
        sprintf(sz_temp, "%d", actualSigNum);
        xmlNewTextChild(root, NULL, BAD_CAST("SignalCount"), BAD_CAST(sz_temp));

        /* save the document data to disk file*/
        int nRel = xmlSaveFile(filePath, doc);
        if (nRel != -1)
        {
            printf("tiosignals.xml: Created Successfully!\n");
        }

        /*release the resources*/
        xmlFreeDoc(doc);
        xmlCleanupParser();
    }
    catch (...)
    {
        printf("save tiosignals failed\n");
    }
#endif
    return 0;
}

int xmlLoadTIOSignals(int* tioSignalNum, GripperStateUnit* gripperSignals)
{
#if 0
    xmlDocPtr pDoc = NULL;
    xmlNodePtr pRoot = NULL;
    xmlNodePtr pCurrNode = NULL;
    const char* filePath = ZUC_USER_CONFIG_DIR "/tiosignals.xml";
    xmlKeepBlanksDefault(0);                                   //必须加上，防止程序把元素前后的空白文本符号当作一个node
    pDoc = xmlReadFile(filePath, "UTF-8", XML_PARSE_RECOVER);  //libxml只能解析UTF-8格式数据
    if (pDoc == NULL)
    {
        rcs_print("xmlLoadTIOSignals: can't open file!\n");
        return -1;
    }

    pRoot = xmlDocGetRootElement(pDoc);
    if (pRoot == NULL || xmlStrcmp(pRoot->name, (const xmlChar*)"Signals") != 0)
    {
        rcs_print("xmlLoadTIOSignals: invalid root node!\n");
        return -1;
    }

    int currSigIndex = 0;
    pCurrNode = pRoot->xmlChildrenNode;
    rcs_print("xmlLoadTIOSignals: node: %s!\n", (const char*)BAD_CAST(pCurrNode->name));
    for (; pCurrNode && currSigIndex < MAX_TIO_SIG_NUM; pCurrNode = pCurrNode->next)
    {
        if (!xmlStrcmp(pCurrNode->name, (const xmlChar*)"Signal"))
        {
            // xmlNodePtr pNode = pCurrNode->xmlChildrenNode;
            xmlChar* sigNameProp = xmlGetProp(pCurrNode, BAD_CAST("Name"));
            if (sigNameProp != NULL)
            {
                strcpy(gripperSignals[currSigIndex].sigName, (const char*)BAD_CAST(sigNameProp));
                xmlFree(sigNameProp);
            }

            xmlChar* chnIdProp = xmlGetProp(pCurrNode, BAD_CAST("ChnId"));
            if (chnIdProp != NULL)
            {
                gripperSignals[currSigIndex].chnId = atoi((const char*)BAD_CAST(chnIdProp));
                xmlFree(chnIdProp);
            }

            xmlChar* typeProp = xmlGetProp(pCurrNode, BAD_CAST("Type"));
            if (typeProp != NULL)
            {
                gripperSignals[currSigIndex].type = atoi((const char*)BAD_CAST(typeProp));
                xmlFree(typeProp);
            }

            xmlChar* addrProp = xmlGetProp(pCurrNode, BAD_CAST("Addr"));
            if (addrProp != NULL)
            {
                gripperSignals[currSigIndex].addr = atoi((const char*)BAD_CAST(addrProp));
                xmlFree(addrProp);
            }
            gripperSignals[currSigIndex].freq = 0.0;
            xmlChar* freqProp = xmlGetProp(pCurrNode, BAD_CAST("Frequency"));
            if (freqProp != NULL)
            {
                gripperSignals[currSigIndex].freq = atof((const char*)BAD_CAST(freqProp));
                xmlFree(freqProp);
            }

            currSigIndex++;
        }
    }

    *tioSignalNum = currSigIndex;
    xmlFreeDoc(pDoc);
    xmlCleanupParser();
    xmlMemoryDump();
#endif
    return 0;
}

int xmlUpdateTIOSignals(const char* filePath, const char* name, const char* attr, int val)
{
#if 0
    xmlDocPtr pDoc = NULL;
    xmlNodePtr pCurrNode = NULL;
    xmlNodePtr pRoot = NULL;

    xmlKeepBlanksDefault(0);
    pDoc = xmlReadFile(filePath, "UTF-8", XML_PARSE_RECOVER);
    if (pDoc == NULL)
    {
        rcs_print("Read the XML file %s failed! \n", filePath);
        return -1;
    }

    pRoot = xmlDocGetRootElement(pDoc);
    if (pRoot == NULL || xmlStrcmp(pRoot->name, BAD_CAST("Signals")) != 0)
    {
        rcs_print("The XML file includes no Signals! \n");
        xmlFreeDoc(pDoc);
        return -1;
    }

    pCurrNode = pRoot->xmlChildrenNode;
    while (pCurrNode != NULL)
    {
        if (xmlStrcmp(xmlGetProp(pCurrNode, BAD_CAST("Name")), name) == 0)
        {
            char freq[4];
            sprintf(freq, "%d", val);
            xmlSetProp(pCurrNode, attr, (const xmlChar*)freq);
        }
        pCurrNode = pCurrNode->next;
    }
    //存储xml文档
    int nRel = xmlSaveFormatFileEnc(filePath, pDoc, "UTF-8", 1);
    if (nRel == -1)
    {
        rcs_print("Save the XML file %s failed! \n", filePath);
    }
    xmlFreeDoc(pDoc);
    xmlCleanupParser();
    xmlMemoryDump();
#endif
    return 0;
}
